package service

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"smart-sensor-cloud/internal/model"
	"smart-sensor-cloud/pkg/util"
	"strconv"
)

func GetLedgers(ctx *gin.Context) {
	sizeStr := ctx.DefaultQuery("size", "10")
	indexStr := ctx.DefaultQuery("page", "1")

	size, err := strconv.Atoi(sizeStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}
	index, err := strconv.Atoi(indexStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("参数转换失败"))
		return
	}

	if results, total, err := model.GetLedgers(index, size); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: "获取台帐列表失败",
		})
		return
	} else {
		util.ResponseOK(ctx, util.OKWithPayload(&util.PaginationResp{
			Size:  size,
			Index: index,
			Data:  results,
			Total: total,
		}))
	}
}

func AddLedger(ctx *gin.Context) {
	ledger := new(model.Ledger)

	if err := ctx.MustBindWith(ledger, binding.JSON); err != nil {
		ctx.Set("error", util.NewInvalidParameterError(err.Error()))
		return
	}

	if err := model.AddLedger(ledger); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OK())
}

func UpdateLedger(ctx *gin.Context) {
	updates := new(model.Ledger)

	if err := ctx.MustBindWith(updates, binding.JSON); err != nil {
		ctx.Set("error", util.NewInvalidParameterError(err.Error()))
		return
	}

	ledgerIDStr := ctx.Param("id")
	if ledgerIDStr == "" {
		ctx.Set("error", util.NewInvalidParameterError("台帐ID不可以为空"))
		return
	}

	ledgerID, err := strconv.Atoi(ledgerIDStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("非法的参数"))
		return
	}

	if err := model.UpdatedLedger(ledgerID, updates); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OK())
}

func GetLedgerByID(ctx *gin.Context) {
	ledgerIDStr := ctx.Param("id")
	if ledgerIDStr == "" {
		ctx.Set("error", util.NewInvalidParameterError("台帐ID不可以为空"))
		return
	}

	ledgerID, err := strconv.Atoi(ledgerIDStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("非法的参数"))
		return
	}

	ledger, err := model.GetLedger(ledgerID)
	if err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OKWithPayload(ledger))
}

func DeleteLedger(ctx *gin.Context) {
	ledgerIDStr := ctx.Param("id")
	if ledgerIDStr == "" {
		ctx.Set("error", util.NewInvalidParameterError("台帐ID不可以为空"))
		return
	}

	ledgerID, err := strconv.Atoi(ledgerIDStr)
	if err != nil {
		ctx.Set("error", util.NewInvalidParameterError("非法的参数"))
		return
	}

	if err := model.DeleteLedger(ledgerID); err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OK())
}
